{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "929ca39a-6641-41d9-bd75-e4b24a7bcf31",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import statsmodels.api as sm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "73552033-48f2-4155-8f79-2a455fa2141b",
   "metadata": {},
   "outputs": [],
   "source": [
    "DATA_PATH = r\"\\df_1.xlsx\"\n",
    "df = pd.read_excel(DATA_PATH)\n",
    "df[\"date\"] = pd.to_datetime(df[\"date\"])\n",
    "df = df.sort_values(\"date\").reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2a48f29b-7e4f-46e0-b064-4b624869a0df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            OLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:              grievance   R-squared:                       0.786\n",
      "Model:                            OLS   Adj. R-squared:                  0.785\n",
      "Method:                 Least Squares   F-statistic:                     150.5\n",
      "Date:                Thu, 01 Jan 2026   Prob (F-statistic):           1.32e-53\n",
      "Time:                        00:10:33   Log-Likelihood:                 1056.4\n",
      "No. Observations:                 598   AIC:                            -2107.\n",
      "Df Residuals:                     595   BIC:                            -2094.\n",
      "Df Model:                           2                                         \n",
      "Covariance Type:                  HAC                                         \n",
      "==============================================================================\n",
      "                 coef    std err          z      P>|z|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "const          0.1012      0.011      9.627      0.000       0.081       0.122\n",
      "China          0.1444      0.009     16.786      0.000       0.128       0.161\n",
      "time           0.0004   5.88e-05      6.429      0.000       0.000       0.000\n",
      "==============================================================================\n",
      "Omnibus:                        2.104   Durbin-Watson:                   0.413\n",
      "Prob(Omnibus):                  0.349   Jarque-Bera (JB):                2.018\n",
      "Skew:                           0.142   Prob(JB):                        0.365\n",
      "Kurtosis:                       3.024   Cond. No.                         444.\n",
      "==============================================================================\n",
      "\n",
      "Notes:\n",
      "[1] Standard Errors are heteroscedasticity and autocorrelation robust (HAC) using 12 lags and without small sample correction\n",
      "\n",
      "Key coefficients (HAC SE, 12 lags):\n",
      "const    0.101224\n",
      "China    0.144375\n",
      "time     0.000378\n",
      "dtype: float64\n",
      "const    0.010514\n",
      "China    0.008601\n",
      "time     0.000059\n",
      "dtype: float64\n",
      "const    6.130843e-22\n",
      "China    3.110581e-63\n",
      "time     1.284822e-10\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "long_df = pd.DataFrame({\n",
    "    \"date\": pd.concat([df[\"date\"], df[\"date\"]], ignore_index=True),\n",
    "    \"grievance\": pd.concat([df[\"grievance_NorthKorea\"], df[\"grievance_China\"]], ignore_index=True),\n",
    "    \"China\": np.concatenate([np.zeros(len(df), dtype=int), np.ones(len(df), dtype=int)])\n",
    "})\n",
    "\n",
    "# Linear monthly time trend: 1,2,3,...,T (same for both NK/China rows)\n",
    "T = df.shape[0]\n",
    "time_trend = np.arange(1, T + 1)\n",
    "long_df[\"time\"] = np.concatenate([time_trend, time_trend])\n",
    "\n",
    "# Drop missing just in case\n",
    "long_df = long_df.dropna(subset=[\"grievance\", \"China\", \"time\"]).reset_index(drop=True)\n",
    "\n",
    "# -----------------------------\n",
    "# 3) OLS with Newey–West HAC standard errors (12-month lag)\n",
    "# -----------------------------\n",
    "X = long_df[[\"China\", \"time\"]]\n",
    "X = sm.add_constant(X)\n",
    "y = long_df[\"grievance\"]\n",
    "\n",
    "model = sm.OLS(y, X).fit(cov_type=\"HAC\", cov_kwds={\"maxlags\": 12})\n",
    "\n",
    "print(model.summary())\n",
    "\n",
    "# Optional: print key coefficients only\n",
    "print(\"\\nKey coefficients (HAC SE, 12 lags):\")\n",
    "print(model.params)\n",
    "print(model.bse)\n",
    "print(model.pvalues)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6603b7a0-0d51-4240-8d91-e18cf0bbe7d8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4de9471-3827-4e49-9810-89645341e12f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5e223f3-fa9e-4b2c-9c8a-e93508771015",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98d3be7a-7bda-4af4-b012-0563e45392ac",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f22cf94-f272-490d-a929-1675bb93b6e8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8fb13de0-ebdb-40c5-b334-75dfdcd70c68",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
